import cTitle from "components/title";
import { scrollMixin } from "../../utils/mixin"; //引入加载更多
import { Toast } from "vant";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      show1: false,
      sort: "asc",
      category: {},
      name: "",

      point: {},
      city: "",
      // more
      isLoadMore: true,
      page: 1,
      total_page: 0,
      titleInfo: "", // 标题
      commentList: [],
      CategoryList: []
    };
  },
  activated() {
    this.init();
    if (this.$route.params.category_id) {
      this.category.id = this.$route.params.category_id;
    }
    this.fun
      .getLocation()
      .then(res => {
        this.point = res.point;
        this.address = res.title;
        this.city = res.city;
        this.search();
      })
      .catch(err => {
        console.log(err);
      });
    this.getCategoryList();
  },
  computed: {
    appointmentLang() {
      if (this.$store.state.shopLanguage.appointment) {
        return this.$store.state.shopLanguage.appointment;
      } else {
        return { worker: "技师", project: "项目", service: "服务" };
      }
    }
  },
  methods: {
    init() {
      this.category = {};
      this.name = "";
      this.sort = "asc";
      this.show1 = false;
      this.page = 1;
      this.isLoadMore = true;
      this.total_page = 0;
      this.commentList = [];
    },
    getCategoryList() {
      $http
        .post("plugin.appointment.frontend.project-category.get-list", {}, "获取中")
        .then(response => {
          if (response.result === 1) {
            this.CategoryList = response.data;
            if (this.$route.params.category_id) {
              this.CategoryList.forEach((ele, ind) => {
                if (ele.id == this.$route.params.category_id) {
                  this.category = ele;
                }
              });
            }
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    selectionBtn(item) {
      console.log(item);
      if (!item.name) {
        this.category = {};
      } else {
        this.category = item;
      }

      this.page = 1;
      this.isLoadMore = true;
      this.total_page = 0;
      this.search();
      this.show1 = false;
    },
    goProjectDetails(id) {
      this.$router.push(this.fun.getUrl("ProjectDetails", { project_id: id }));
    },
    enterSearch(event) {
      if (event.keyCode === 13) {
        //如果按的是enter键
        event.preventDefault();
        this.page = 1;
        this.isLoadMore = true;
        this.total_page = 0;
        this.search();
      }
    },
    search(page) {
      if (page) {
        this.page = page;
        this.isLoadMore = true;
        this.total_page = 0;
      }
      $http
        .post(
          "plugin.appointment.frontend.project.get-project-list-new",
          {
            lng: this.point.lng,
            lat: this.point.lat,
            city_name: this.city,
            sort: this.sort,
            category_id: this.category.id,
            name: this.name,
            page: this.page
          },
          "获取中"
        )
        .then(response => {
          this.fun.setWXTitle(this.appointmentLang.project + "详情");
          if (response.result === 1) {
            this.isLoadMore = true;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.commentList = response.data.data;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    showClass() {
      this.show1 ? (this.show1 = false) : (this.show1 = true);
    },
    evaluateBtn() {
      if (this.sort == "asc") {
        this.sort = "desc";
      } else {
        this.sort = "asc";
      }
      this.show1 = false;
      this.page = 1;
      this.isLoadMore = true;
      this.total_page = 0;
      this.search();
    },
    getMoreData() {
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http
          .post(
            "plugin.appointment.frontend.project.get-project-list-new",
            {
              lng: this.point.lng,
              lat: this.point.lat,
              city_name: this.city,
              sort: this.sort,
              category_id: this.category.id,
              name: this.name,
              page: this.page
            },
            "加载中"
          )
          .then(
            function(response) {
              that.isLoadMore = true;
              if (response.result === 1) {
                var myData = response.data.data;
                that.commentList = that.commentList.concat(myData); //数组拼接
              } else {
                that.page = that.page - 1;
                that.isLoadMore = false;
                return;
              }
            },
            function(response) {
              // error callback
            }
          );
      }
    }
  },
  components: { cTitle }
};
